import {createRouter, createWebHistory} from 'vue-router'
import routes from './routes'

import {createRoutes} from "@/router/autoload";
import NProgress from 'nprogress'
// 引入对应css样式
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export async function setupRouter(app) {

  let childRoutes =  createRoutes()
  childRoutes.forEach(x => {
    router.addRoute('layout',x)
  })
  app.use(router)
}

router.beforeEach(async (to, from, next) => {
  const token = localStorage.getItem('TOKEN')
  NProgress.start()

  if (to.path === '/login') {
    next()
    return false
  } else {
    if (token) {

    }
  }

  if (!token) {
    next({
      path: '/login'
    })
    return false
  }
  next()
})

router.afterEach((to) => {
  NProgress.done()
})

export default router
